package com.example.controller;

import com.example.pojo.dto.BookDeleteDTO;
import com.example.pojo.dto.BookInsertDTO;
import com.example.pojo.dto.BookListDTO;
import com.example.pojo.dto.BookUpdateDTO;
import com.example.pojo.entity.result.PageResult;
import com.example.pojo.entity.result.Result;
import com.example.pojo.vo.BookListVO;
import com.example.pojo.vo.BookVO;
import com.example.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/book")
@RequiredArgsConstructor
@Slf4j
@Api("图书相关接口")
public class BookController {
    private final BookService bookService;

    /**
     * 图书入库
     *
     * @param bookInsertDTO
     * @return
     */
    @PostMapping
    @ApiOperation("图书入库")
    public Result insertBook(@RequestBody BookInsertDTO bookInsertDTO) {
        log.info("图书入库：{}", bookInsertDTO);
        bookService.insertBookWithCategory(bookInsertDTO);
        log.info("入库成功");
        return Result.success();
    }

    /**
     * 根据图书ID删除图书
     *
     * @param bookDeleteDTO
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除图书")
    public Result deleteBook(BookDeleteDTO bookDeleteDTO) {
        log.info("删除图书：{}", bookDeleteDTO);
        bookService.deleteBook(bookDeleteDTO);
        log.info("删除成功");
        return Result.success();
    }

    /**
     * 根据图书分类id分页查询图书列表
     *
     * @param bookListDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据图书分类id分页查询图书列表")
    public Result<PageResult<BookListVO>> listByCategoryId(BookListDTO bookListDTO) {
        log.info("根据图书分类id分页查询图书列表：{}", bookListDTO);
        PageResult<BookListVO> bookListVOPageResult = bookService.listByCategoryId(bookListDTO);
        log.info("查询成功：{}", bookListVOPageResult);
        return Result.success(bookListVOPageResult);
    }

    /**
     * 根据图书编号查询图书信息
     *
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据图书编号查询图书信息")
    public Result<BookVO> getById(@PathVariable String id) {
        log.info("根据图书编号查询图书信息：{}", id);
        BookVO bookVO = bookService.getById(id);
        log.info("查询成功：{}", bookVO);
        return Result.success(bookVO);
    }

    /**
     * 修改图书信息
     *
     * @param bookUpdateDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改图书信息")
    public Result updateById(@RequestBody BookUpdateDTO bookUpdateDTO) {
        log.info("修改图书信息：{}", bookUpdateDTO);
        bookService.updateById(bookUpdateDTO);
        log.info("修改成功");
        return Result.success();
    }


}
